<template>
  <div class="app-container">
    <el-card class="filter-container" shadow="never">
      <div>
        <i class="el-icon-search"></i>
        <span>筛选搜索</span>
        <el-button style="float:right" type="primary" @click="initdata()" size="small">查询搜索</el-button>
        <el-button style="float:right;margin-right: 15px" @click="resetfiled()" size="small">重置</el-button>
      </div>
      <div style="margin-top: 15px">
        <el-form :inline="true" :model="listQuery" size="small" label-width="140px">
          <el-form-item label="管理处名称">
            <el-input v-model="listQuery.name" class="input-width" placeholder="请输入管理处名称" clearable></el-input>
          </el-form-item>
          <!-- <el-form-item label="管理处id">
            <el-input v-model="listQuery.id" class="input-width" placeholder="请输入管理处id" clearable></el-input>
          </el-form-item>-->
        </el-form>
      </div>
    </el-card>
    <el-card class="operate-container" shadow="never">
      <i class="el-icon-tickets"></i>
      <span>数据列表</span>
      <el-button size="mini" class="btn-add" @click="isadd()" style="margin-left: 20px">添加</el-button>
    </el-card>

    <div style="margin:0px;">
      <el-table :data="tableData" border stripe>
        <el-table-column prop="name" label="管理处名称" align="center"></el-table-column>
        <el-table-column prop="createTime" label="创建时间" align="center">
          <template slot-scope="scope">{{scope.row.createTime | formattime}}</template>
        </el-table-column>

        <el-table-column label="操作" align="center" width="180">
          <template slot-scope="scope">
            <!-- <el-button type="primary" @click="delrecord(scope.row)">附加属性</el-button> -->
            <el-button type="primary" @click="delrecord(scope.row)">删除</el-button>
            <el-button type="primary" @click="showupdaterecord(scope)">更新</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div style="text-align:center;" v-loading="loading">
      <el-pagination
        @size-change="sizeChange"
        @current-change="currentChange"
        :current-page="currentPage"
        :page-sizes="[5, 10, 20, 50]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="totalNum"
        background
      ></el-pagination>
    </div>
    <el-dialog
      :title="isupdate ? '更新' : '添加'"
      :visible.sync="isadddia"
      center
      width="40%"
      @close="isadddia = false"
    >
      <el-form
        :model="refform"
        ref="refform"
        label-width="120px"
        style="margin: 0px 70px;"
        :rules="rules"
      >
        <el-form-item label="管理处名称" prop="name">
          <el-input v-model="refform.name"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-button @click="isadddia = false">取 消</el-button>
        <el-button type="primary" @click="isupdate ? updaterecord() : addrecord()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import managementService from "@/api/managementService";

import dayjs from "dayjs";

export default {
  components: {},
  data() {
    return {
      loading: true,
      listQuery: {
        name: "",
        id: ""
      },
      pageSize: 5,
      currentPage: 1,
      totalNum: 0,
      isupdate: false,
      isshowqr: false,
      qrdata: "",
      tableData: [],
      isadddia: false,
      refform: {
        name: ""
      },
      rules: {
        name: [{ required: true, message: "不能为空", trigger: "blur" }]
      }
    };
  },
  computed: {},
  watch: {},
  mounted() {
    this.initdata();
  },
  filters: {
    formattime(val) {
      return dayjs(val).format("YYYY-MM-DD HH:mm:ss");
    }
  },
  methods: {
    showupdaterecord(scope) {
      this.isupdate = true;
      this.refform = _.cloneDeep(scope.row);
      this.isadddia = true;
    },
    addattr() {
      let attr = {
        customTag: "",
        name: "",
        value: ""
      };
      this.refform.attrs.push(attr);
    },
    delattr() {
      this.refform.attrs.pop();
    },
    sizeChange(val) {
      this.pageSize = val;
      this.initdata();
    },
    currentChange(val) {
      this.currentPage = val;
      this.initdata();
    },

    delrecord(row) {
      managementService.del({ ids: row.id }).then(res => {
        this.initdata();
        this.$message.success("删除成功");
      });
    },
    updaterecord() {
      this.$refs.refform.validate(valid => {
        if (valid) {
          managementService.update(this.refform).then(res => {
            this.isadddia = false;
            this.initdata();
            this.$message.success("修改成功");
          });
        }
      });
    },
    initdata() {
      this.loading = true;
      managementService
        .select({
          // id: this.listQuery.id,
          page: this.currentPage,
          count: this.pageSize,
          name: this.listQuery.name
        })
        .then(res => {
          this.tableData = res.data.records;
          this.totalNum = res.data.total;
          // this.$message.success("数据加载成功");
          this.loading = false;
        });
    },
    isadd() {
      this.isadddia = true;
      this.isupdate = false;
      this.resetinputdia();
    },

    resetinputdia() {
      this.refform = { name: "" };
    },

    resetfiled() {
      this.listQuery.name = "";
    },

    addrecord() {
      // this.isadddia = true
      this.$refs.refform.validate(valid => {
        if (valid) {
          managementService.add(this.refform).then(res => {
            this.isadddia = false;
            this.$message.success("添加成功");
            this.initdata();
          });
        }
      });
    }
  }
};
</script>
<style lang='scss' scoped>
</style>